# Copyright (c) 2021 Intel Corporation
#
# SPDX-License-Identifier: Apache-2.0

ARG IMAGE_REGISTRY=docker.io
FROM ubuntu

ENV GO_HOME="/opt"
ENV GOCACHE="${GO_HOME}/.cache"
ENV RUSTUP_HOME="/opt/rustup"
ENV CARGO_HOME="/opt/cargo"
ENV PATH="/opt/cargo/bin/:/opt/go/bin:${PATH}"

ARG GO_VERSION="1.22.11"
ARG RUST_TOOLCHAIN="1.80.0"
ARG ARCH="aarch64"
ENV PATH="/opt/go/bin:${PATH}"

SHELL ["/bin/bash", "-o", "pipefail", "-c"]

RUN mkdir ${RUSTUP_HOME} ${CARGO_HOME} ${GOCACHE} && \
    chmod -R a+rwX ${RUSTUP_HOME} ${CARGO_HOME} ${GO_HOME}

# makedev tries to mknod from postinst
RUN [ -x /usr/bin/systemd-detect-virt ] || ( echo "echo docker" >/usr/bin/systemd-detect-virt && chmod +x /usr/bin/systemd-detect-virt )
# hadolint ignore=DL3009,SC2046
RUN apt-get update && \
    DEBIAN_FRONTEND=noninteractive \
    apt-get --no-install-recommends -y install \
    ca-certificates \
    curl \
    g++ \
    $(gcc_arch="${ARCH}" && [ "$(uname -m)" != "$gcc_arch" ] && ( \
         libc_arch="$gcc_arch" && \
         [ "$gcc_arch" = aarch64 ] && libc_arch=arm64; \
         [ "$gcc_arch" = ppc64le ] && gcc_arch=powerpc64le && libc_arch=ppc64el; \
         [ "$gcc_arch" = s390x ] && gcc_arch=s390x && libc_arch=s390x; \
         [ "$gcc_arch" = x86_64 ] && gcc_arch=x86-64 && libc_arch=amd64; \
         echo "gcc-$gcc_arch-linux-gnu libc6-dev-$libc_arch-cross")) \
    git \
    gnupg2 \
    libclang-dev \
    make \
    makedev \
    multistrap \
    musl \
    musl-dev \
    musl-tools \
    protobuf-compiler \
    xz-utils \
    pip \
    python3-dev \
    libclang-dev \
    zstd \
    ninja-build \
    vim \
    bzip2 \
    pkg-config \
    libglib2.0-dev \
    libmount-dev \
	libssl-dev

RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain ${RUST_TOOLCHAIN}

RUN ARCH=$(uname -m); \
    goarch=""; \
    kernelname=$(uname -s | tr '[:upper:]' '[:lower:]'); \
    case "${ARCH}" in \
        "aarch64") goarch="arm64" ;; \
        "ppc64le") goarch=${ARCH} ;; \
        "x86_64") goarch="amd64" ;; \
        "s390x") goarch=${ARCH} ;; \
        *) echo "Unsupported architecture: ${ARCH}" && exit 1 ;; \
    esac; \
    curl -OL "https://storage.googleapis.com/golang/go${GO_VERSION}.${kernelname}-${goarch}.tar.gz" && \
    tar -C "${GO_HOME}" -xzf "go${GO_VERSION}.${kernelname}-${goarch}.tar.gz" && \
    rm "go${GO_VERSION}.${kernelname}-${goarch}.tar.gz"

# aarch64 requires this name -- link for all
RUN if [ ! -f "/usr/bin/$(uname -m)-linux-musl-gcc" ]; then ln -s /usr/bin/musl-gcc "/usr/bin/$(uname -m)-linux-musl-gcc"; fi

RUN ARCH=$(uname -m); \
	rust_arch=""; \
    libc=""; \
    case "${ARCH}" in \
        "aarch64") rust_arch="${ARCH}"; libc="musl"; ;; \
        "ppc64le") rust_arch="powerpc64le"; libc="gnu"; ;; \
        "x86_64") rust_arch="${ARCH}"; libc="musl"; ;; \
        "s390x") rust_arch="${ARCH}"; libc="gnu"; ;; \
        *) echo "Unsupported architecture: ${ARCH}" && exit 1 ;; \
	esac; \
    rustup target add "${rust_arch}-unknown-linux-${libc}"

RUN pip install --no-cache-dir pyinstaller==6.9.0 --break-system-packages
RUN pip install Sphinx --break-system-packages
